package vip.idiy.anwei_android_iot.views;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.Nullable;

import vip.idiy.anwei_android_iot.R;

/**
 * ClassName LayoutBasicInfo
 * describe:
 * Created: 勇
 * Email:118085890@qq.com
 * Version: 1.0
 * Create in 16:04 2023/4/19
 */
public class LayoutBasicInfo extends LinearLayout {

    private LinearLayout layout_reuse_basic_info_linear;
    private ImageView layout_reuse_basic_info_image_left;
    private TextView layout_reuse_basic_info_text_view_title;
    private LinearLayout layout_reuse_basic_info_linear_a;
    private TextView layout_reuse_basic_info_text_view_content;
    private Switch layout_reuse_basic_info_switch;
    private RelativeLayout layout_reuse_basic_info_linear_b;
    private ImageView layout_reuse_basic_info_image_centre;
    private ImageView layout_reuse_basic_info_image_right;
    private LinearLayout layout_reuse_basic_info_linear_c;
    private EditText layout_reuse_basic_info_edit_view_content;

    private String centreIconName;


    public LayoutBasicInfo(Context context) {
        super(context);
    }

    public LayoutBasicInfo(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * @param leftImg  左边图片
     * @param a        中间内容和开关是否显示
     * @param b        中间图片是否显示
     * @param c        中间编辑框是否显示
     * @param switchShow 开关是否显示
     * @param rightImg 右边图片
     * @return
     */
    public LayoutBasicInfo init(boolean leftImg, boolean a, boolean b, boolean c, boolean switchShow, boolean rightImg) {
        LayoutInflater.from(getContext()).inflate(R.layout.layout_reuse_basic_info, this, true);
        // 整行
        layout_reuse_basic_info_linear = findViewById(R.id.layout_reuse_basic_info_linear);

        // 左边图标
        layout_reuse_basic_info_image_left = findViewById(R.id.layout_reuse_basic_info_image_left);
        // 是否显示左边图片
        layout_reuse_basic_info_image_left.setVisibility(leftImg ? View.VISIBLE : View.INVISIBLE);

        // 左边文本
        layout_reuse_basic_info_text_view_title = findViewById(R.id.layout_reuse_basic_info_text_view_title);

        // 包含中间内容和开关显示
        layout_reuse_basic_info_linear_a = findViewById(R.id.layout_reuse_basic_info_linear_a);
        // 中间内容和开关显示是否显示
        layout_reuse_basic_info_linear_a.setVisibility(a ? View.VISIBLE : View.GONE);
        if (a) {
            // 中间内容
            layout_reuse_basic_info_text_view_content = findViewById(R.id.layout_reuse_basic_info_text_view_content);
            // 开关
            layout_reuse_basic_info_switch = findViewById(R.id.layout_reuse_basic_info_switch);
            layout_reuse_basic_info_switch.setVisibility(switchShow ? View.VISIBLE : View.GONE);
        }

        // 包含显示中间图片
        layout_reuse_basic_info_linear_b = findViewById(R.id.layout_reuse_basic_info_linear_b);
        layout_reuse_basic_info_linear_b.setVisibility(b ? View.VISIBLE : View.GONE);
        if (b)
            // 中间图片
            layout_reuse_basic_info_image_centre = findViewById(R.id.layout_reuse_basic_info_image_centre);

        // 中间编辑框
        layout_reuse_basic_info_linear_c = findViewById(R.id.layout_reuse_basic_info_linear_c);
        layout_reuse_basic_info_linear_c.setVisibility(c ? View.VISIBLE : View.GONE);
        if (c)
            // 中间编辑
            layout_reuse_basic_info_edit_view_content = findViewById(R.id.layout_reuse_basic_info_edit_view_content);


        // 右边图片
        layout_reuse_basic_info_image_right = findViewById(R.id.layout_reuse_basic_info_image_right);
        layout_reuse_basic_info_image_right.setVisibility(rightImg ? View.VISIBLE : View.INVISIBLE);
        return this;
    }

    /**
     * 设置左边图标
     *
     * @param img
     * @return
     */
    public LayoutBasicInfo setLeftImg(int img) {
        layout_reuse_basic_info_image_left.setImageResource(img);
        return this;
    }

    /**
     * 设置左边文本
     *
     * @param text
     * @return
     */
    public LayoutBasicInfo setLeftText(String text) {
        layout_reuse_basic_info_text_view_title.setText(text);
        return this;
    }

    /**
     * 设置中间的文本
     *
     * @param text
     * @return
     */
    public LayoutBasicInfo setCentreContent(String text) {
        layout_reuse_basic_info_text_view_content.setText(text);
        return this;
    }


    /**
     * 设置switch的状态
     *
     * @param status
     * @return
     */
    public LayoutBasicInfo setSwitchStatus(boolean status) {
        layout_reuse_basic_info_switch.setChecked(status);
        return this;
    }

    /**
     * 获取switch的状态
     * @return
     */
    public boolean getSwitchStatus(){
        return layout_reuse_basic_info_switch.isChecked();
    }

    /**
     * 设置中间图标
     *
     * @param img
     * @return
     */
    public LayoutBasicInfo setCentreImg(int img) {
        layout_reuse_basic_info_image_centre.setImageResource(img);
        return this;
    }

    /**
     * 设置右边图标
     *
     * @param img
     * @return
     */
    public LayoutBasicInfo setRightImg(int img) {
        layout_reuse_basic_info_image_right.setImageResource(img);
        return this;
    }

    /**
     * 设置整行点击事件
     *
     * @param click
     * @return
     */
    public LayoutBasicInfo setLineClick(OnClickListener click) {
        layout_reuse_basic_info_linear.setOnClickListener(click);
        return this;
    }

    /**
     * 设置整行点击效果
     *
     * @param drawable
     * @return
     */
    public LayoutBasicInfo setLineBackground(int drawable) {
        layout_reuse_basic_info_linear.setBackgroundResource(drawable);
        return this;
    }

    /**
     * 获取中间编辑框的内容
     * @return
     */
    public String getCentreEditContent(){
        return layout_reuse_basic_info_edit_view_content.getText().toString().trim();
    }

    /**
     * 设置中间编辑框内容
     * @param text
     * @return
     */
    public LayoutBasicInfo setCentreEditContent(String text){
        layout_reuse_basic_info_edit_view_content.setText(text);
        return this;
    }


    /**
     * 获取中间图标名称
     * @return
     */
    public String getCentreIconName(){
        return this.centreIconName;
    }

    /**
     * 设置中间图标
     * @param iconName
     */
    public void setCentreIconName(String iconName){
        this.centreIconName = iconName;
    }

    // 三种模式使用
    // 1. 中间显示文本
    /**
    LayoutBasicInfo la = new LayoutBasicInfo(this)
            .init(true, true, false, false, true, true)
            .setLeftImg(R.mipmap.icon_cancel_off)
            .setLeftText("标题1")
            .setCentreContent("测试内容1")
            .setSwitchStatus(true)
            .setRightImg(R.mipmap.icon_carbarn)
            .setLineBackground(R.drawable.ripple_gray)
            .setLineClick(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i(TAG, "onClick: a");
                }
            });
    */
    // 2. 中间显示图标
    /**
    LayoutBasicInfo lb = new LayoutBasicInfo(this)
            .init(true, false, true, false, false, true)
            .setLeftImg(R.mipmap.icon_cancel_off)
            .setLeftText("标题2")
            .setCentreImg(R.mipmap.icon_bedroom)
            .setRightImg(R.mipmap.icon_carbarn)
            .setLineBackground(R.drawable.ripple_gray)
            .setLineClick(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i(TAG, "onClick: b");
                }
            });
    */

    // 3.
    /**
    LayoutBasicInfo lc = new LayoutBasicInfo(this)
            .init(true, false, false, true, false, false)
            .setLeftText("标题3")
            .setLineBackground(R.drawable.ripple_gray)
            .setLeftImg(R.mipmap.icon_drawing);
    */
}
